from drf_spectacular.extensions import OpenApiViewExtension
from drf_spectacular.utils import extend_schema, OpenApiParameter, OpenApiExample
from drf_spectacular.types import OpenApiTypes


class VehicleViewSetOpenApi(OpenApiViewExtension):
    target_class = 'vehicle_app.views.VehicleViewSet'

    def view_replacement(self):
        from rest_framework.decorators import action
        from .serializers import (
            VehicleSerializer, VehicleCreateSerializer, VehicleUpdateSerializer,
            VehicleLocationUpdateSerializer, VehicleListSerializer
        )

        class Extended(self.target_class):
            @extend_schema(
                summary="获取车辆列表",
                description="""获取车辆列表，支持按状态和车辆类型筛选，以及搜索和排序。
                
                默认只返回状态为空闲的车辆，可以通过status参数获取其他状态的车辆。
                可以通过search参数搜索车牌号或当前位置。
                可以通过ordering参数指定排序字段，如created_at、capacity等。
                """,
                parameters=[
                    OpenApiParameter(
                        name='status',
                        description='车辆状态',
                        type=str,
                        enum=['available', 'in_transit', 'maintenance', 'unavailable']
                    ),
                    OpenApiParameter(
                        name='vehicle_type',
                        description='车辆类型',
                        type=str,
                        enum=['tank_truck', 'flat_bed', 'box_truck', 'other']
                    ),
                    OpenApiParameter(
                        name='search',
                        description='搜索关键词（车牌号或当前位置）',
                        type=str
                    ),
                    OpenApiParameter(
                        name='ordering',
                        description='排序字段（如created_at、capacity等）',
                        type=str
                    ),
                    OpenApiParameter(
                        name='personal',
                        description='是否只返回当前用户车辆',
                        type=bool
                    ),
                ],
                responses={
                    200: VehicleListSerializer(many=True)
                },
                tags=['Vehicles'],
                examples=[
                    OpenApiExample(
                        '车辆列表响应示例',
                        summary='车辆列表响应示例',
                        description='返回车辆列表的示例数据',
                        value=[
                            {
                                "id": 1,
                                "user_name": "13800138000",
                                "license_plate": "粤A12345",
                                "vehicle_type": "tank_truck",
                                "vehicle_type_display": "罐车",
                                "capacity": "20.00",
                                "current_location": "广东省广州市天河区",
                                "status": "available",
                                "status_display": "空闲",
                                "available_time": "2025-10-25T08:00:00Z",
                                "created_at": "2025-10-20T10:00:00Z"
                            }
                        ]
                    )
                ]
            )
            def list(self):
                pass

            @extend_schema(
                summary="获取车辆详情",
                description="""根据车辆ID获取车辆的详细信息，包括车辆图片等。
                
                特殊说明：
                - 当ID为'me'时，返回当前司机用户的第一辆车辆信息
                - 当ID为具体数字时，返回对应ID的车辆信息
                """,
                responses={
                    200: VehicleSerializer
                },
                tags=['Vehicles'],
                examples=[
                    OpenApiExample(
                        '车辆详情响应示例',
                        summary='车辆详情响应示例',
                        description='返回车辆详细信息的示例数据',
                        value={
                            "id": 1,
                            "user": {
                                "id": 1,
                                "phone": "13800138000",
                                "role": "driver",
                                "avatar": "https://example.com/avatar.jpg",
                                "company": "ABC运输公司",
                                "credit_score": "4.8"
                            },
                            "license_plate": "粤A12345",
                            "vehicle_type": "tank_truck",
                            "vehicle_type_display": "罐车",
                            "capacity": "20.00",
                            "length": "12.50",
                            "width": "2.50",
                            "height": "3.00",
                            "current_location": "广东省广州市天河区",
                            "longitude": "113.280661",
                            "latitude": "23.125178",
                            "location_updated_at": "2025-10-21T10:00:00Z",
                            "status": "available",
                            "status_display": "空闲",
                            "available_time": "2025-10-25T08:00:00Z",
                            "vehicle_license": "LD123456789012",
                            "vehicle_license_image": "https://example.com/license.jpg",
                            "images": [
                                {
                                    "id": 1,
                                    "image_url": "https://example.com/vehicle_front.jpg",
                                    "description": "车辆正面",
                                    "image_type": "exterior",
                                    "image_type_display": "车辆外观",
                                    "created_at": "2025-10-20T10:00:00Z"
                                }
                            ],
                            "created_at": "2025-10-20T10:00:00Z",
                            "updated_at": "2025-10-21T10:00:00Z"
                        }
                    )
                ]
            )
            def retrieve(self):
                pass

            @extend_schema(
                summary="创建或更新车辆信息",
                description="""创建新车辆或更新现有车辆信息。
                
                如果指定车牌号的车辆已存在，则更新该车辆信息；否则创建新车辆。
                支持同时上传车辆图片信息。
                
                注意事项：
                - 只有认证的司机用户才能调用此接口
                - 可以通过images参数传递图片URL或直接上传图片文件
                - 如果上传图片文件，系统会自动将其存储到腾讯云COS并生成访问URL
                """,
                request={
                    'multipart/form-data': {
                        'type': 'object',
                        'properties': {
                            'license_plate': {
                                'type': 'string',
                                'description': '车牌号'
                            },
                            'vehicle_type': {
                                'type': 'string',
                                'enum': ['tank_truck', 'flat_bed', 'box_truck', 'other'],
                                'description': '车辆类型'
                            },
                            'capacity': {
                                'type': 'number',
                                'description': '载重量(吨)'
                            },
                            'length': {
                                'type': 'number',
                                'description': '车长(米)'
                            },
                            'width': {
                                'type': 'number',
                                'description': '车宽(米)'
                            },
                            'height': {
                                'type': 'number',
                                'description': '车高(米)'
                            },
                            'current_location': {
                                'type': 'string',
                                'description': '当前位置'
                            },
                            'longitude': {
                                'type': 'number',
                                'description': '经度'
                            },
                            'latitude': {
                                'type': 'number',
                                'description': '纬度'
                            },
                            'status': {
                                'type': 'string',
                                'enum': ['available', 'in_transit', 'maintenance', 'unavailable'],
                                'description': '状态'
                            },
                            'available_time': {
                                'type': 'string',
                                'format': 'date-time',
                                'description': '可用时间'
                            },
                            'vehicle_license': {
                                'type': 'string',
                                'description': '行驶证号'
                            },
                            'vehicle_license_image': {
                                'type': 'string',
                                'format': 'binary',
                                'description': '行驶证照片'
                            },

                        }
                    }
                },
                responses={
                    200: OpenApiTypes.OBJECT,
                    201: OpenApiTypes.OBJECT
                },
                tags=['Vehicles'],
                examples=[
                    OpenApiExample(
                        '创建车辆请求示例',
                        summary='创建车辆请求示例',
                        description='创建车辆的请求参数示例',
                        value={
                            "license_plate": "粤A12345",
                            "vehicle_type": "tank_truck",
                            "capacity": "20.00",
                            "length": "12.50",
                            "width": "2.50",
                            "height": "3.00",
                            "current_location": "广东省广州市天河区",
                            "longitude": "113.280661",
                            "latitude": "23.125178",
                            "status": "available",
                            "available_time": "2025-10-25T08:00:00Z",
                            "vehicle_license": "LD123456789012",
                            "vehicle_license_image": "https://example.com/license.jpg"
                        }
                    ),
                    OpenApiExample(
                        '创建车辆响应示例',
                        summary='创建车辆响应示例',
                        description='创建车辆成功的响应示例',
                        value={
                            "message": "车辆信息创建成功",
                            "vehicle": {
                                "id": 1,
                                "user": {
                                    "id": 1,
                                    "phone": "13800138000",
                                    "role": "driver",
                                    "avatar": "https://example.com/avatar.jpg",
                                    "company": "ABC运输公司",
                                    "credit_score": "4.8"
                                },
                                "license_plate": "粤A12345",
                                "vehicle_type": "tank_truck",
                                "vehicle_type_display": "罐车",
                                "capacity": "20.00",
                                "length": "12.50",
                                "width": "2.50",
                                "height": "3.00",
                                "current_location": "广东省广州市天河区",
                                "longitude": "113.280661",
                                "latitude": "23.125178",
                                "location_updated_at": "2025-10-21T10:00:00Z",
                                "status": "available",
                                "status_display": "空闲",
                                "available_time": "2025-10-25T08:00:00Z",
                                "vehicle_license": "LD123456789012",
                                "vehicle_license_image": "https://example.com/license.jpg",
                                "created_at": "2025-10-20T10:00:00Z",
                                "updated_at": "2025-10-21T10:00:00Z"
                            }
                        }
                    ),
                    OpenApiExample(
                        '更新车辆响应示例',
                        summary='更新车辆响应示例',
                        description='更新车辆成功的响应示例',
                        value={
                            "message": "车辆信息更新成功",
                            "vehicle": {
                                "id": 1,
                                "user": {
                                    "id": 1,
                                    "phone": "13800138000",
                                    "role": "driver",
                                    "avatar": "https://example.com/avatar.jpg",
                                    "company": "ABC运输公司",
                                    "credit_score": "4.8"
                                },
                                "license_plate": "粤A12345",
                                "vehicle_type": "tank_truck",
                                "vehicle_type_display": "罐车",
                                "capacity": "20.00",
                                "length": "12.50",
                                "width": "2.50",
                                "height": "3.00",
                                "current_location": "广东省广州市天河区",
                                "longitude": "113.280661",
                                "latitude": "23.125178",
                                "location_updated_at": "2025-10-21T10:00:00Z",
                                "status": "available",
                                "status_display": "空闲",
                                "available_time": "2025-10-25T08:00:00Z",
                                "vehicle_license": "LD123456789012",
                                "vehicle_license_image": "https://example.com/license.jpg",
                                "created_at": "2025-10-20T10:00:00Z",
                                "updated_at": "2025-10-21T10:00:00Z"
                            }
                        }
                    )
                ]
            )
            @action(detail=False, methods=['post'])
            def create_or_update(self):
                pass

            @extend_schema(
                summary="更改车辆状态",
                description="""更改指定车辆的状态，如空闲、运输中、维护中或不可用。
                
                特殊说明：
                - 当车辆ID为'me'时，更改当前司机用户的第一辆车辆状态
                """,
                request={
                    'application/json': {
                        'type': 'object',
                        'properties': {
                            'status': {
                                'type': 'string',
                                'enum': ['available', 'in_transit', 'maintenance', 'unavailable'],
                                'description': '车辆状态'
                            }
                        },
                        'required': ['status']
                    }
                },
                responses={
                    200: VehicleSerializer
                },
                tags=['Vehicles'],
                examples=[
                    OpenApiExample(
                        '更改状态请求示例',
                        summary='更改状态请求示例',
                        description='更改车辆状态的请求参数示例',
                        value={
                            "status": "in_transit"
                        }
                    ),
                    OpenApiExample(
                        '更改状态响应示例',
                        summary='更改状态响应示例',
                        description='更改车辆状态成功的响应示例',
                        value={
                            "message": "车辆状态更新成功",
                            "vehicle": {
                                "id": 1,
                                "user": {
                                    "id": 1,
                                    "phone": "13800138000",
                                    "role": "driver",
                                    "avatar": "https://example.com/avatar.jpg",
                                    "company": "ABC运输公司",
                                    "credit_score": "4.8"
                                },
                                "license_plate": "粤A12345",
                                "vehicle_type": "tank_truck",
                                "vehicle_type_display": "罐车",
                                "capacity": "20.00",
                                "length": "12.50",
                                "width": "2.50",
                                "height": "3.00",
                                "current_location": "广东省广州市天河区",
                                "longitude": "113.280661",
                                "latitude": "23.125178",
                                "location_updated_at": "2025-10-21T10:00:00Z",
                                "status": "in_transit",
                                "status_display": "运输中",
                                "available_time": "2025-10-25T08:00:00Z",
                                "vehicle_license": "LD123456789012",
                                "vehicle_license_image": "https://example.com/license.jpg",

                                "created_at": "2025-10-20T10:00:00Z",
                                "updated_at": "2025-10-21T10:00:00Z"
                            }
                        }
                    )
                ]
            )
            @action(detail=True, methods=['post'])
            def change_status(self):
                pass

        return Extended